package com.siteseat.crudeq.kernel.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import com.siteseat.common.foundation.Component;
import com.siteseat.crudeq.kernel.Chunk;
import com.siteseat.crudeq.kernel.ChunkFileFilter;
import com.siteseat.crudeq.kernel.ChunkInfo;
import com.siteseat.crudeq.kernel.ChunkManager;
import com.siteseat.crudeq.kernel.ChunkWriter;
import com.siteseat.crudeq.kernel.KernelFactory;
import com.siteseat.crudeq.kernel.Message;
import com.siteseat.crudeq.kernel.MessageHistory;
import com.siteseat.crudeq.kernel.Queue;
import com.siteseat.crudeq.kernel.QueueProfile;

/**
 *
 */
public class KernelFactoryImpl extends Component implements KernelFactory {

  /**
   * {@inheritDoc}
   */
  @Override
  public PersistedChunkManager newPersistedChunkManager( QueueProfile profile ) throws Exception {
    PersistedChunkManager r =  new PersistedChunkManager( profile );
    r.setFactory( this );
    return r;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Queue newQueue( QueueProfile profile, ChunkManager chunkManager ) throws Exception {
    return new QueueImpl( profile, chunkManager );
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public Chunk newChunk( ChunkInfo info, int capacity ) throws IOException {
    return new Chunk( this, info, capacity );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public ChunkFileFilter newChunkFileFilter( QueueProfile profile ) {
    return new ChunkFileFilter( this, profile );
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public ChunkInfo newChunkInfo( QueueProfile prof, Long chunkId ) throws IOException {
    return new ChunkInfo( prof, chunkId );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public ChunkInfo newChunkInfo( QueueProfile prof, File file, Long chunkId ) throws FileNotFoundException {
    return new ChunkInfo( prof, file, chunkId );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public ChunkWriter newChunkWriter( ChunkInfo info ) throws IOException {
    return new ChunkWriter( info );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Message newMessage( Chunk s, int offset, int messageSize ) {
    return new Message( s, offset, messageSize );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public MessageHistory newMessageHistory( Integer queueId, List<ChunkInfo> chunks, int offset ) {
    return new MessageHistory( queueId, chunks, offset );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected void doDestroy() throws Exception {
    // Do nothing so far
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected void doInit() throws Exception {
    // Do nothing so far
  }

}
